<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- hash -->
    <!-- onHashChange -->

    <a href="#/home">首页</a>
    <a href="#/list">列表</a>
    <a href="#/my">我的</a>

    <div id="box"></div>

    <script>
        class Router {
            constructor(props) {
                this.routes = props.routes
                this.box = document.getElementById('box')
                this.events = {}
            }
            init() {
               this.routes.forEach(item => {
                  this.events[item.path] = () => {
                    this.box.innerHTML = item.com
                  }
               })
               window.addEventListener('hashchange', () => {
                   let hash = location.hash.slice(1)
                   this.changeHash(hash)
               })
            }
            changeHash(hash) {
                this.events[hash]()
            }
         }

        let router = new Router({
            routes: [
                {
                    path: "/home",
                    com: "我是首页"
                },
                {
                    path: "/list",
                    com: "我是列表"
                },
                {
                    path: "/my",
                    com: "我的页面"
                }
            ]
        })

        router.init()
    </script>
</body>
</html>